package jse.io.filter;

import org.junit.Test;

import java.io.*;

/**
* @desc   测试输入流
* @author kanggw
* @datetime 2016/8/16,14:49
*/
public class TestInputStream {

    /**
    * @desc   通过read方法循环读取。
    * @author kanggw
    * @datetime 2016/8/16,14:59
    */
    @Test
    public void test1() {

        //找到文件
        File file = new File("c:"+File.separator+"aaa.txt");
        //建立连接
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //读取
        try {
            int next =   0;
            while ((next = inputStream.read()) != -1) {
                System.out.println((char) next);
            }
        //关闭
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
    * @desc   通过字节数组缓冲读取
    * @author kanggw
    * @datetime 2016/8/16,15:00
    */
    @Test
    public void test2() {

        //找到文件
        File file = new File("c:"+File.separator+"aaa.txt");
        //建立连接
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //读取
        try {
            byte[] bytes = new byte[4];
            int num =  0;
            while ((num = inputStream.read(bytes, 0, bytes.length)) != -1) {
                System.out.print(new String(bytes));
            }
            //关闭
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
